#!/bin/bash
#绿色解压、修改内容
#1，安装git工具
#2，下载、安装


#####需求替换
TOOLS="git"
SOFT="tomcat"
VER="group"
#VER2="1.8.0"
SER1="tomcat1"
SER2="tomcat2"
SER3="tomcat3"

#RPMNAME="tomcat"




#####无需替换
GITEXT=".git"
FILEEXT="_init.sh"

TAG="."
TAG1=".*"
NUM="[0-9]"


SHELLDIR="/opt/shell/"
SOFTDIR="/opt/"$SOFT"-"$VER"/"

GITADDR="https://gitee.com/ffing/"
GITDOWN=""$GITADDR""$SOFT"-"$VER""$GITEXT""

LOCALDIR="/usr/local/"

IPLAN=$(hostname -I | awk -F " " '{printf $1}')
IPWAN=$(curl http://pv.sohu.com/cityjson 2>> /dev/null | awk -F '"' '{print $4}')


#####安装TOOLS类
pName=$(rpm -qa | grep "^${TOOLS}${TAG}${NUM}${TAG}${NUM}")
if [ $? -eq 0 ]
then
       echo -e	"----------已存在，无需安装----------\n"${pName}""
	   echo
else
echo "----------正在安装 "${TOOLS}"……"
yum -y install $TOOLS
               if [ $? -eq 0 ]
               then
                        echo "----------安装完成 "${TOOLS}""
						echo
               else
                        echo -e "\e[41m >>>错误>>>安装失败 "${TOOLS}"\e[0m"
						echo
               fi
fi

#####检测java
java -version 2>/dev/null
if [ $? -ne 0  ];then
echo "开始安装JAVA"
bash <(curl https://gitee.com/ffing/onekey/raw/master/java-8_init.sh)
fi



#####下载、安装

#版本号类型
pName=$(rpm -qa | grep "^${SOFT}${TAG}${NUM}${TAG}${NUM}")
#名称类型
#pName=$(rpm -qa | grep "^${SOFT}${TAG1}${RPMNAME}")
if [ $? -eq 0 ]
then
	   echo -e "\e[31m !!!!!!  安装失败  !!!!!! \e[0m"
      echo -e	"----------已存在，请先卸载----------\n"${pName}""
	   echo
else
		echo "----------开始下载 "${SOFT}"……"
		if [ ! -d $SOFTDIR ];then
		echo "创建${SOFT}下载目录 ${SOFTDIR}"
		git clone $GITDOWN $SOFTDIR 
		
		else
		echo "存在${SOFT}下载目录 ${SOFTDIR}"
		fi
		
		echo "----------Copy "${SOFT}" 到 /usr/local/……"
		\cp -r ${SOFTDIR} ${LOCALDIR}

				echo "----------添加"${SOFT}"环境变量----------"
				cat /etc/profile |grep TOMCAT_HOME_1= 
       		if [ $? -eq 0 ]
       		then
      		sed -i 's/TOMCAT_HOME_1=.*/TOMCAT_HOME_1=\/usr\/local\/tomcat-group\/tomcat-8_1/g' /etc/profile
      		sed -i 's/CATALINA_HOME_1=.*/CATALINA_HOME_1=\/usr\/local\/tomcat-group\/tomcat-8_1/g' /etc/profile
      		sed -i 's/CATLINA_BASE_1=.*/CATLINA_BASE_1=\/usr\/local\/tomcat-group\/tomcat-8_1/g' /etc/profile

      		sed -i 's/TOMCAT_HOME_2=.*/TOMCAT_HOME_2=\/usr\/local\/tomcat-group\/tomcat-8_2/g' /etc/profile
      		sed -i 's/CATALINA_HOME_2=.*/CATALINA_HOME_2=\/usr\/local\/tomcat-group\/tomcat-8_2/g' /etc/profile
      		sed -i 's/CATLINA_BASE_2=.*/CATLINA_BASE_2=\/usr\/local\/tomcat-group\/tomcat-8_2/g' /etc/profile

      		sed -i 's/TOMCAT_HOME_3=.*/TOMCAT_HOME_3=\/usr\/local\/tomcat-group\/tomcat-8_3/g' /etc/profile
      		sed -i 's/CATALINA_HOME_3=.*/CATALINA_HOME_3=\/usr\/local\/tomcat-group\/tomcat-8_3/g' /etc/profile
      		sed -i 's/CATLINA_BASE_3=.*/CATLINA_BASE_3=\/usr\/local\/tomcat-group\/tomcat-8_3/g' /etc/profile
				else
				#插入全局变量
				cat ${SOFTDIR}export >> /etc/profile
				fi



				source /etc/profile

				#配置systemctl
				cat ${SOFTDIR}tomcat1.service > /usr/lib/systemd/system/tomcat1.service
				cat ${SOFTDIR}tomcat2.service > /usr/lib/systemd/system/tomcat2.service
				cat ${SOFTDIR}tomcat3.service > /usr/lib/systemd/system/tomcat3.service
				systemctl daemon-reload
				
				#防火墙放行端口
				echo “防火墙放行tomcat端口：8180、8280、8380”
				sed -i '/<\/zone>/i     <port protocol="tcp" port="8180"\/>' /etc/firewalld/zones/public.xml
				sed -i '/<\/zone>/i     <port protocol="tcp" port="8280"\/>' /etc/firewalld/zones/public.xml
				sed -i '/<\/zone>/i     <port protocol="tcp" port="8380"\/>' /etc/firewalld/zones/public.xml
				firewall-cmd --reload

				#重启服务
				systemctl restart $SER1 $SER2 $SER3 > /dev/null 2>&1
				systemctl enable $SER1 $SER2 $SER3 > /dev/null 2>&1


				#####服务状态

				echo "----------JAVA 版本信息----------"
      		java -version

				
				systemctl status $SER1 >/dev/null
				if [ $? -eq 0 ];then
      		echo 
      		echo -e "\e[42m "${SER1}" 			已经启动\e[0m"
      		echo "  内网访问：http://"$IPLAN":8180"
				echo "  外网访问：http://"$IPWAN":8180"
				echo "  启动、停止、重启：systemct [start|stop|restart] $SER1"
				echo "  tomcat1目录：/usr/local/tomcat-group/tomcat-8_1"
				else
				echo -e "\e[41m "${SER1}" 			启动失败\e[0m"
				fi



				systemctl status $SER2 >/dev/null
				if [ $? -eq 0 ];then
      		echo 
      		echo -e "\e[42m "${SER2}" 			已经启动\e[0m"
      		echo "  内网访问：http://"$IPLAN":8280"
				echo "  外网访问：http://"$IPWAN":8280"
				echo "  启动、停止、重启：systemct [start|stop|restart] $SER2"
				echo "  tomcat2目录：/usr/local/tomcat-group/tomcat-8_2"
				else
				echo -e "\e[41m "${SER2}" 			启动失败\e[0m"
				fi


				systemctl status $SER3 >/dev/null
				if [ $? -eq 0 ];then
      		echo 
      		echo -e "\e[42m "${SER3}" 			已经启动\e[0m"
      		echo "  内网访问：http://"$IPLAN":8380"
				echo "  外网访问：http://"$IPWAN":8380"
				echo "  启动、停止、重启：systemct [start|stop|restart] $SER3"
				echo "  tomcat3目录：/usr/local/tomcat-group/tomcat-8_3"
				else
				echo -e "\e[41m "${SER3}" 			启动失败\e[0m"
				fi

fi


